1
|
|
|
'use strict'; |
2
|
|
|
|
3
|
|
|
var expect = require('expect.js'); |
4
|
|
|
|
5
|
|
|
var Position = require('../lib/position'); |
6
|
|
|
var Grid = require('../lib/grid'); |
7
|
|
|
var Mower = require('../lib/mower'); |
8
|
|
|
var Move = require('../lib/move'); |
9
|
|
|
|
10
|
|
|
describe('Move', function() { |
11
|
|
|
describe('runInstruction', function() { |
12
|
|
|
var grid = new Grid(new Position({x:5,y:5}, 'N')), |
13
|
|
|
position = new Position({x:2,y:2}, 'E'), |
14
|
|
|
mower = new Mower(0, grid, position, ['A']), |
15
|
|
|
move = new Move(mower, grid); |
16
|
|
|
|
17
|
|
|
it('should run instruction ’A’ with cardinal ’E’ and change mower’s position to: x+1', function (done) { |
18
|
|
|
move.runInstruction('A').then(function(newPosition) { |
19
|
|
|
expect(newPosition.x).to.equal(position.x+1); |
20
|
|
|
done(); |
21
|
|
|
}); |
22
|
|
|
}); |
23
|
|
|
it('should run instruction ’A’ with cardinal ’W’ and change mower’s position to: x-1', function (done) { |
24
|
|
|
var startPosition = new Position({x:2,y:2}, 'W'); |
25
|
|
|
move.mower.position = startPosition; |
26
|
|
|
move.runInstruction('A').then(function(newPosition) { |
27
|
|
|
expect(newPosition.x).to.equal(startPosition.x-1); |
28
|
|
|
done(); |
29
|
|
|
}); |
30
|
|
|
}); |
31
|
|
|
it('should run instruction ’A’ with cardinal ’N’ and change mower’s position to: y+1', function (done) { |
32
|
|
|
var startPosition = new Position({x:2,y:2}, 'N'); |
33
|
|
|
move.mower.position = startPosition; |
34
|
|
|
move.runInstruction('A').then(function(newPosition) { |
35
|
|
|
expect(newPosition.y).to.equal(startPosition.y+1); |
36
|
|
|
done(); |
37
|
|
|
}); |
38
|
|
|
}); |
39
|
|
|
it('should run instruction ’A’ with cardinal ’S’ and change mower’s position to: y-1', function (done) { |
40
|
|
|
var startPosition = new Position({x:2,y:2}, 'S'); |
41
|
|
|
move.mower.position = startPosition; |
42
|
|
|
move.runInstruction('A').then(function(newPosition) { |
43
|
|
|
expect(newPosition.y).to.equal(startPosition.y-1); |
44
|
|
|
done(); |
45
|
|
|
}); |
46
|
|
|
}); |
47
|
|
|
it('should run instruction ’G’ with cardinal ’N’ and change mower’s position to: c=’W’', function (done) { |
48
|
|
|
move.mower.position = new Position({x:2,y:2}, 'N'); |
49
|
|
|
move.runInstruction('G').then(function(newPosition) { |
50
|
|
|
expect(newPosition.c).to.equal('W'); |
51
|
|
|
done(); |
52
|
|
|
}); |
53
|
|
|
}); |
54
|
|
|
it('should run instruction ’G’ with cardinal ’S’ and change mower’s position to: c=’E’', function (done) { |
55
|
|
|
move.mower.position = new Position({x:2,y:2}, 'S'); |
56
|
|
|
move.runInstruction('G').then(function(newPosition) { |
57
|
|
|
expect(newPosition.c).to.equal('E'); |
58
|
|
|
done(); |
59
|
|
|
}); |
60
|
|
|
}); |
61
|
|
|
it('should run instruction ’D’ with cardinal ’N’ and change mower’s position to: c=’E’', function (done) { |
62
|
|
|
move.mower.position = new Position({x:2,y:2}, 'N'); |
63
|
|
|
move.runInstruction('D').then(function(newPosition) { |
64
|
|
|
expect(newPosition.c).to.equal('E'); |
65
|
|
|
done(); |
66
|
|
|
}); |
67
|
|
|
}); |
68
|
|
|
it('should run instruction ’D’ with cardinal ’S’ and change mower’s position to: c=’W’', function (done) { |
69
|
|
|
move.mower.position = new Position({x:2,y:2}, 'S'); |
70
|
|
|
move.runInstruction('D').then(function(newPosition) { |
71
|
|
|
expect(newPosition.c).to.equal('W'); |
72
|
|
|
done(); |
73
|
|
|
}); |
74
|
|
|
}); |
75
|
|
|
}); |
76
|
|
View Code Duplication |
describe('updatePosition', function() { |
|
|
|
|
77
|
|
|
var grid = new Grid(new Position({x:5,y:5}, 'N')), |
78
|
|
|
position = new Position({x:2,y:2}, 'E'), |
79
|
|
|
mower = new Mower(0, grid, position, ['A']), |
80
|
|
|
move = new Move(mower, grid); |
81
|
|
|
|
82
|
|
|
it('should update a position when the target position is not occupied', function () { |
83
|
|
|
var newPosition = new Position({x:position.x+1,y:position.y}, 'E'); |
84
|
|
|
move.updatePosition(newPosition); |
85
|
|
|
expect(move.mower.position.x).to.equal(position.x+1); |
86
|
|
|
}); |
87
|
|
|
it('should not update a position when the target position is occupied', function () { |
88
|
|
|
move.mower.position = new Position({x:2,y:2}, 'E'); // set mower's position |
89
|
|
|
move.grid.occupation[move.mower.id] = move.mower.position; // set current mower's occupation |
90
|
|
|
move.grid.occupation[1] = new Position({x:3,y:2}, 'E'); // add fake item in object grid.occupation |
91
|
|
|
var newPosition = new Position({x:position.x+1,y:position.y}, 'E'); // position to try |
92
|
|
|
move.updatePosition(newPosition); // prevent moving if the target cell is occupied by another item by excluding this item from the comparision |
93
|
|
|
expect(move.mower.position.x).to.equal(position.x); // X still unchanged |
94
|
|
|
}); |
95
|
|
|
|
96
|
|
|
}); |
97
|
|
View Code Duplication |
describe('goForward', function() { |
|
|
|
|
98
|
|
|
var grid = new Grid(new Position({x:5,y:5}, 'N')), |
99
|
|
|
position = new Position({x:2,y:2}, 'E'), |
100
|
|
|
mower = new Mower(0, grid, position, ['A']), |
101
|
|
|
move = new Move(mower, grid); |
102
|
|
|
|
103
|
|
|
it('should move mower to 1 cell forward with cardinal ’E’ and change mower’s position to: x+1', function () { |
104
|
|
|
move.goForward(); |
105
|
|
|
expect(move.mower.position.x).to.equal(position.x+1); |
106
|
|
|
}); |
107
|
|
|
it('should move mower to 1 cell forward with cardinal ’W’ and change mower’s position to: x-1', function () { |
108
|
|
|
move.mower.position = new Position({x:2,y:2}, 'W'); |
109
|
|
|
move.goForward(); |
110
|
|
|
expect(move.mower.position.x).to.equal(position.x-1); |
111
|
|
|
}); |
112
|
|
|
it('should move mower to 1 cell forward with cardinal ’N’ and change mower’s position to: y+1', function () { |
113
|
|
|
move.mower.position = new Position({x:2,y:2}, 'N'); |
114
|
|
|
move.goForward(); |
115
|
|
|
expect(move.mower.position.y).to.equal(position.y+1); |
116
|
|
|
}); |
117
|
|
|
it('should move mower to 1 cell forward with cardinal ’S’ and change mower’s position to: y-1', function () { |
118
|
|
|
move.mower.position = new Position({x:2,y:2}, 'S'); |
119
|
|
|
move.goForward(); |
120
|
|
|
expect(move.mower.position.y).to.equal(position.y-1); |
121
|
|
|
}); |
122
|
|
|
|
123
|
|
|
}); |
124
|
|
|
describe('turn', function() { |
125
|
|
|
var grid = new Grid(new Position({x:10,y:10}, 'N')), |
126
|
|
|
position = new Position({x:2,y:2}, 'E'), |
127
|
|
|
mower = new Mower(0, grid, position, ['G']), |
128
|
|
|
move = new Move(mower, grid); |
129
|
|
|
|
130
|
|
|
it('should move mower to the left with cardinal ’E’ and change mower’s position to: c=’N’', function () { |
131
|
|
|
move.turn('G'); |
132
|
|
|
expect(move.mower.position.c).to.equal('N'); |
133
|
|
|
}); |
134
|
|
|
it('should move mower to the left with cardinal ’N’ and change mower’s position to: c=’W’', function () { |
135
|
|
|
move.mower.position = new Position({x:2,y:2}, 'N'); |
136
|
|
|
move.turn('G'); |
137
|
|
|
expect(move.mower.position.c).to.equal('W'); |
138
|
|
|
}); |
139
|
|
|
it('should move mower to the right with cardinal ’E’ and change mower’s position to: c=’S’', function () { |
140
|
|
|
move.mower.position = new Position({x:2,y:2}, 'E'); |
141
|
|
|
move.turn('D'); |
142
|
|
|
expect(move.mower.position.c).to.equal('S'); |
143
|
|
|
}); |
144
|
|
|
it('should move mower to the right with cardinal ’W’ and change mower’s position to: c=’N’', function () { |
145
|
|
|
move.mower.position = new Position({x:2,y:2}, 'W'); |
146
|
|
|
move.turn('D'); |
147
|
|
|
expect(move.mower.position.c).to.equal('N'); |
148
|
|
|
}); |
149
|
|
|
|
150
|
|
|
}); |
151
|
|
|
}); |